import time
import logging
from common import Dnconsole

import logging

logging.basicConfig(
    filename="canhx_log.log",
    # format='%(asctime)s - %(name)s - %(pathname)s - %(lineno)d - %(levelname)s - %(message)s',
    # datefmt='%d-%b-%y %H:%M:%S',
    level=logging.INFO
)


def main():
    # 请根据自己电脑配置
    # # console = 'D:\\Changzhi\\dnplayer2\\dnconsole.exe '
    # console = 'C:\\leidian\\LDPlayer9\\dnconsole.exe'
    # # ld = 'D:\\Changzhi\\dnplayer2\\ld.exe '
    # ld = 'D:\\leidian\\LDPlayer9\\ld.exe '
    # # share_path = 'F:/share/Pictures'
    # share_path = 'C:/Users/Leq/Documents/leidian9/Pictures'
    dnconsole = Dnconsole()

    # # 查看可用的模拟器列表
    # ld_list = dnconsole.get_list()
    # # print("INFO: 可用的模拟器列表:\n {}".format(ld_list))
    #
    # logging.info("INFO: 可用的模拟器列表:\n {}".format(ld_list))
    #
    # # 查看正在运行的模拟器
    # run_list = dnconsole.list_running()
    # # print("INFO: 正在运行的模拟器列表:\n {}".format(run_list))
    # logging.info("INFO: 正在运行的模拟器列表:\n {}".format(run_list))

    # # 启动模拟器
    # res = dnconsole.launch(0)
    # print(res)
    # time.sleep(5)

    # # 设置分辨率
    # dnconsole.set_screen_size(0)

    # # 看安装的软件列表
    # dnconsole.get_package_list(0)

    # # 打开“长安幻想”
    # res = dnconsole.invokeapp(0, "com.syyx.cahx.ld")
    # print(res)
    # time.sleep(5)

    # 等待图片出现后点击
    # img_list = [
    #     "./changanhuanxiang/images/01001.png",  # 1_公告_开屏_1
    #     "./changanhuanxiang/images/01002.png",  # 踏入长安
    # ]
    # have_img_tag = dnconsole.check_picture_and_touch(0, img_list, [1000, 120], 0.5)
    # print(have_img_tag)
    img_list = [
        # ["./changanhuanxiang/images/01001.png", [1000, 120], "开屏公告"],
        # ["./changanhuanxiang/images/01002.png", [500, 1700], "踏入长安"],
        ["./changanhuanxiang/images/01003.png", [500, 1700], "进入长安"],
        ["./changanhuanxiang/images/01004.png", [500, 1550], "第n章"],
        ["./changanhuanxiang/images/01005.png", [1080, 1820], "跳过"],  # 有跳过就需要点多次
        # ["./changanhuanxiang/images/01006.png", [1000, 1700], "点击空白处关闭"],
        # ["./changanhuanxiang/images/01007.png", [1200, 1900], "右下角"],
        # ["./changanhuanxiang/images/01008.png", [30, 1800], "左下角返回"],
        # ["./changanhuanxiang/images/01009.png", [500, 1800], "战斗"],
        # ["./changanhuanxiang/images/01010.png", [1030, 1730], "可领取"],
        # ["./changanhuanxiang/images/01010.png", [900, 450], "领取"],
        # ["./changanhuanxiang/images/01012.png", [500, 1800], "进入对决"],
        # ["./changanhuanxiang/images/01013.png", [1000, 1910], "自动"],
        # ["./changanhuanxiang/images/01014.png", [509, 600], "圆"],
    ]
    # dnconsole.check_picture_and_touch(0, ["./changanhuanxiang/images/01002.png"], img_list, 0.5)
    for img in img_list:
        # have_img_tag = dnconsole.check_picture_and_touch(0, img[0], img[1], 0.5)
        try:
            have_img_and_touched_tag = dnconsole.check_picture_and_touch(0, [img[0]], img[1], 0.08)
            print(have_img_and_touched_tag)
            if have_img_and_touched_tag[0] == 0:
                print("INFO: {}".format(img[2]))
                logging.info(img[2])
        except Exception as e:
            print(e)
        # print(have_img_tag)
    # dnconsole.touch(0, 800, 300)
    # 等待页面出现特定图片
    # have_img = dnconsole.wait_picture(0, 5, r"C:/Users/Leq/Documents/leidian9/Pictures/testpng_1.png", 0.5)

    # # 查找一系列图片，查到后返回（图片需要按优先级做好排序）
    # img_list = [
    #     # r"C:/Users/Leq/Documents/leidian9/Pictures/01001.png",  # 1_公告_开屏_1
    #     "./changanhuanxiang/images/01000.png",  # 1_公告_开屏_1
    #     "./changanhuanxiang/images/01001.png",  # 1_公告_开屏_1
    #     # [r"C:/Users/Leq/Documents/leidian9/Pictures/01001.png", [1000, 120]],  # 1_公告_开屏_1
    # ]
    # # res = dnconsole.check_picture_and_touch(0, img_list[0][0], img_list[0][1], 0.5)
    # # print(res)
    #
    # res = dnconsole.check_picture(0, img_list, 0.5)
    # # print(type(res))
    # print(res[0])
    # if res[0] == 0:
    #     print("INFO: 找到元素")
    #     # dnconsole.check_picture_and_touch(0, img_list[0], img_list[1])
    # else:
    #     print("ERROR: 没找到")
    # time.sleep(5)
    # time.sleep(25)
    # dnconsole.touch(0, 1000, 120)  # 公告
    # time.sleep(5)
    # dnconsole.touch(0, 500, 1700)  # 踏入长安
    # time.sleep(3)
    # dnconsole.touch(0, 500, 1700)  # 进入长安
    # dnconsole.touch(0, 500, 1800)  # 第一章

    # 把需要点击的内容的id排序（根据优先级）

    # # 关闭app
    # res = dnconsole.stopapp(0, "com.syyx.cahx.ld")
    # print(res)


if __name__ == '__main__':
    main()
